import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class addTab implements MouseListener {
    JPanel jp;//标题面板
    JLabel lab ;
    JLabel lab3=new JLabel();//删除按钮
    public addTab(){
        lab= new JLabel("选项卡1");
        JLabel lab1 = new JLabel("选项卡" + tab.i++);//标题

        //创建面板
        jp=new JPanel();
        GridLayout gl= new GridLayout(1,1,10,0);
        jp.setLayout(gl);
        lab1.setHorizontalAlignment(JLabel.LEFT);//设置标题显示在最左边
        lab3.setHorizontalAlignment(JLabel.RIGHT);// 设置按钮显示在最右边
        jp.add(lab1);
        jp.add(lab3);

        //往选择夹中添加子项
        tab.pane.addTab(null,lab);

        //设置标题
        System.out.println(tab.pane.indexOfComponent(lab));
        tab.pane.setTabComponentAt(tab.pane.indexOfComponent(lab),jp);//实现这个功能的就这一条最重要的语句
        lab3.addMouseListener(this);

    }

    @Override

    public void mouseClicked(MouseEvent arg0) {

        // TODO Auto-generated method stub

        tab.pane.remove(tab.pane.indexOfTabComponent(jp));

    }

    @Override

    public void mouseEntered(MouseEvent e) {

        // TODO Auto-generated method stub

        lab3.setText("x ");

    }

    @Override

    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub
        lab3.setText("");
    }

    @Override

    public void mousePressed(MouseEvent arg0) {

        // TODO Auto-generated method stub

    }

    @Override

    public void mouseReleased(MouseEvent arg0) {

        // TODO Auto-generated method stub

    }


}

public class tab extends JFrame implements ActionListener {
    static int i = 0;
    //创建菜单
    JMenuItem mi;
    //创建选择夹
    static JTabbedPane pane;
    public tab(){
        //创建窗口
        super("选项卡窗格");
        setVisible(true);
        setSize(800,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //菜单增加菜单项
        mi=new JMenuItem("增加");
        JMenu menu=new JMenu("增加");
        menu.add(mi);
        JMenuBar bar=new JMenuBar();
        bar.add(menu);
        setJMenuBar(bar);

        //往窗口中加入JTabbedPane
        pane = new JTabbedPane();
        this.add(pane);

        //添加选择夹子项
        new addTab();
        mi.addActionListener(this);

    }









    public static void main(String[] args) {
        tab a = new tab();
        a.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {

                super.keyTyped(e);
            }

            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println(e.isControlDown());
                System.out.println(e.getKeyChar());
                super.keyPressed(e);
            }

            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
            }
        });

        ;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == mi) {
            new addTab();
        }
    }
}
